package org.mybatis.generator.utils;

import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

/**
 * <b><code>XMLMapperGeneratorHelper</code></b>
 * <p/>
 * Description: XML MAPPER生成器
 * <p/>
 * <b>Creation Time:</b> 2020/06/08.
 *
 */
public class XMLMapperGeneratorHelper {

	/**
	 * The constant INSERT.
	 */
	public static final String INSERT = "insert";

	/**
	 * The constant DELETE.
	 */
	public static final String DELETE = "delete";

	/**
	 * The constant UPDATE.
	 */
	public static final String UPDATE = "update";

	/**
	 * The constant SELECT.
	 */
	public static final String SELECT = "select";

	/**
	 * The constant RESULT_TYPE.
	 */
	public static final String RESULT_TYPE = "resultType";

	/**
	 * The constant SELECT.
	 */
	public static final String RESULT_MAP = "resultMap";

	/**
	 * xmlElement构造器.
	 * 
	 * @param sqlElementType
	 * @param sqlMapperId
	 * @param introspectedTable
	 * @param parameterType
	 * @param resultType
	 * @return
	 */
	public static XmlElement baseElementGenerator(String sqlElementType, String sqlMapperId, String parameterType,
			String resultTypeAttr, String resultType) {
		XmlElement baseElement = new XmlElement(sqlElementType);

		baseElement.addAttribute(new Attribute("id", sqlMapperId));

		if (null != parameterType) {
			baseElement.addAttribute(new Attribute("parameterType", parameterType));
		}

		if (null != resultTypeAttr) {
			baseElement.addAttribute(new Attribute(resultTypeAttr, resultType));
		}
		/*	if (null != resultType) {
		if (RESULT_TYPE.equals(resultType)) {
			if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
				baseElement.addAttribute(new Attribute(RESULT_TYPE, introspectedTable.getRecordWithBLOBsType()));
			} else {
				baseElement.addAttribute(new Attribute(RESULT_TYPE, introspectedTable.getBaseRecordType()));
			}
		} else {
			if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
				baseElement.addAttribute(new Attribute(RESULT_MAP, introspectedTable.getResultMapWithBLOBsId()));
			} else {
				baseElement.addAttribute(new Attribute(RESULT_MAP, introspectedTable.getBaseResultMapId()));
			}
		}
	}*/
		return baseElement;
	}

	/**
	 * foreach构造器.
	 * 
	 * @param collectionName
	 * @param itemName
	 * @param indexName
	 * @param separatorName
	 * @return
	 */
	public static XmlElement baseForeachElementGenerator(String collectionName, String itemName, String indexName,
			String separatorName) {
		return baseForeachElementGenerator(collectionName, itemName, indexName, null, null, separatorName);
	}

	/**
	 * foreach构造器.
	 * 
	 * @param collectionName
	 * @param itemName
	 * @param indexName
	 * @param openPrefix
	 * @param closeSuffix
	 * @param separatorName
	 * @return
	 */
	public static XmlElement baseForeachElementGenerator(String collectionName, String itemName, String indexName,
			String openPrefix, String closeSuffix, String separatorName) {
		XmlElement foreachElement = new XmlElement("foreach");
		if (null != collectionName) {
			foreachElement.addAttribute(new Attribute("collection", collectionName));
		}
		if (null != itemName) {
			foreachElement.addAttribute(new Attribute("item", itemName));
		}
		if (null != indexName) {
			foreachElement.addAttribute(new Attribute("index", indexName));
		}
		if (null != openPrefix && null != closeSuffix) {
			foreachElement.addAttribute(new Attribute("open", openPrefix));
			foreachElement.addAttribute(new Attribute("close", closeSuffix));
		}
		if (null != separatorName) {
			foreachElement.addAttribute(new Attribute("separator", separatorName));
		}
		return foreachElement;
	}

	/**
	 * if 构造器.
	 * 
	 * @param columnJavaTypeName
	 * @param sql
	 * @param judgeNull
	 * @return
	 */
	public static XmlElement baseIfJudgeElementGen(String columnJavaTypeName, String sql, boolean judgeNull) {
		String colmunJudge = "";
		if (judgeNull) {
			colmunJudge = columnJavaTypeName + " ==null ";
		} else {
			colmunJudge = columnJavaTypeName + " !=null ";
		}
		XmlElement ifElement = new XmlElement("if");
		ifElement.addAttribute(new Attribute("test", colmunJudge));
		ifElement.addElement(new TextElement(sql));
		return ifElement;
	}

	/**
	 * trim 构造器.
	 * 
	 * @param prefix
	 * @param suffix
	 * @param suffixOverrides
	 * @return
	 */
	public static XmlElement baseTrimElement(String prefix, String suffix, String suffixOverrides) {
		XmlElement trimElement = new XmlElement("trim");
		if (null != prefix) {
			trimElement.addAttribute(new Attribute("prefix", prefix));
		}
		if (null != suffix) {
			trimElement.addAttribute(new Attribute("suffix", suffix));
		}
		if (null != suffixOverrides) {
			trimElement.addAttribute(new Attribute("suffixOverrides", suffixOverrides));
		}
		return trimElement;
	}
}